Custom react frontend
Connect Algolia to your headless Salesforce B2C Commerce using a React frontend.
This guide describes how to integrate Algolia into a headless Salesforce B2C Commerce storefront using a custom React frontend.
To set up Algolia for your headless Salesforce B2C Commerce storefront, see Headless commerce.
Set up a React project
This guide uses Next.js but you can apply the instructions to any React project.
Create a new Next.js app with the create-next-app
command-line tool.
Running this command creates a new project in the directory my-react-storefront
and starts a local development server on http://localhost:3000/
.
Build your search experience
To create the search experience, you can either:
- Use the Unified InstantSearch package.
- Build a custom experience with React InstantSearch.
This guide uses the Unified InstantSearch package.
Download and run Unified InstantSearch
Clone the Unified InstantSearch GitHub repository.
Configure Unified InstantSearch for your data
Open the file unified-instantsearch-ecommerce/src/config/settings.js
and make the following changes:
-
Add your Algolia application ID, search API key, and index name:
-
Configure your replica indices for sorting by “price” in ascending and descending order:
-
Configure your category attributes.
By default, the Algolia cartridge indexes the primary category on the attributes
__primary_category.*
:js -
Set your price attribute.
By default, the Algolia cartridge indexes prices on the attributes
price.${currency}
: -
Optional: remove query suggestions.
If you don’t have Query Suggestions set up, remove the following code:
-
Customize the
<Hit>
component:jsx
The <Hit>
component includes code for sending click events when users select a product in the search results.
Export the project
To make your frontend available in your storefront, you need to export it:
Running this command creates a new directory unified-instantsearch-ecommerce/export
with all assets.
Copy this directory to the public
directory of your storefront project.
Create a new <Search />
component
To use your React frontend in your storefront, create a new <Search />
component in the components
directory.
Add the following code to the file: Search.js
:
Use your <Search />
component in your storefront
Open the file pages/index.js
with your <Home />
component and add your <Search />
component to it.
Click and conversion events
To complete your setup, send click and conversion events.